package com.delay.queue.delayedqueue.rabbitmq;

import com.rabbitmq.client.Channel;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author gxing
 * @desc 消费者
 * @date 2021/3/22
 */
@Component
public class MessageConsumer {
    private static final Logger logger = LogManager.getLogger(MessageConsumer.class);

    /**
     * 监听消息
     *
     * @param message
     * @param channel
     * @param msgBody
     */
    @RabbitListener(queues = RabbitMQConfig.DEAD_LETTER_QUEUE_NAME)
    public void deadLetterListener(Message message, Channel channel, String msgBody) {
        String msg = new String(message.getBody());
        logger.info("receive message:{}, currentTime:{}, ", msgBody, System.currentTimeMillis() / 1000);
//        try {
//            //.....处理业务......
//            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
//        } catch (IOException e) {
//            channel.basicNack(message.getMessageProperties().getDeliveryTag(), true, true);
//            e.printStackTrace();
//        }
    }

    /**
     * 监听消息
     *
     * @param message
     * @param channel
     * @param msgBody
     */
    @RabbitListener(queues = RabbitMQConfig.X_DELAYED_QUEUE_NAME)
    public void xDelayedListener(Message message, Channel channel, String msgBody) {
        String msg = new String(message.getBody());
        logger.info("receive message:{}, currentTime:{}, ", msgBody, System.currentTimeMillis() / 1000);
//        try {
//            //.....处理业务......
//            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
//        } catch (IOException e) {
//            channel.basicNack(message.getMessageProperties().getDeliveryTag(), true, true);
//            e.printStackTrace();
//        }
    }

}
